<template>
  <div>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model.trim="dataForm.name" placeholder="名称"> </el-input>
      </el-form-item>
      <el-form-item label="附件" :label-width="formLabelWidth" prop="icon">
        <el-upload class="upload-demo" :action="url + '/file/uploadFileInterface'" :headers="headers"
          :before-upload="beforeAvatarUpload" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList"
          :on-success="handleSuccess" :on-change="handleChange">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="排序" :label-width="formLabelWidth" prop="orderNum">
        <el-input-number v-model="dataForm.orderNum" :min="0" controls-position="right" label="排序号">
        </el-input-number>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="dataFormSubmit()" :disabled="isDisabled">确 定</el-button>
      </span>
    </template>
  </el-dialog>
  <el-dialog title="视频" :visible.sync="qrcodeDialogVisible" width="540px">
    <video controls muted loop :src="videoUrl" width="500">
    </video>
    <span slot="footer" class="dialog-footer">
      <el-button @click="qrcodeDialogVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import { getToken } from "@/utils/auth";
export default {
  emits: ["refreshDataList"],
  data() {
    return {
      visible: false,
      dialogFormVisible: false,
      qrcodeDialogVisible:false,
      isDisabled: false,
      isval: false,
      isupdate: 0,
      istype: "",
      dataForm: {
        id: 0,
        name: '',//用户单位
        appendixUrl: '',//附件路径
        appendixName: '',//附件名称
        orderNum: 0,
      },
      fileList: [],
      url: process.env.VUE_APP_BASE_API,
      headers: {
        token: getToken(), //从cookie里获取token，并赋值  Authorization ，而不是token
        // withCredentials: false,
      },
      videoUrl:'',
      dataRule: {
        driverName: [
          { required: true, message: '司机姓名必填' },
        ],
        driverPhone: [
          { required: true, message: '司机联系电话必填' },
        ],
        licenseNumber: [
          { required: true, message: '车牌号必须填写' },
        ]
      },
      formLabelWidth: "120px",
      visible: false,
      // url: process.env.VUE_APP_BASE_API,
    };
  },
  created() { },
  methods: {
    async init(id) {
      this.dataForm.id = id || 0;
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.fileList=[]
        this.dataForm.productModel = "";
        if (!this.dataForm.id) {
          // 新增
          // this.treeListTreeSetCurrentNode()
        } else {
          //获取详情
          this.Sevice({
            url: "/syn/service/info/" + this.dataForm.id,
            method: "get",
          }).then(async (res) => {
            // console.log(res);
            if (res.code != 200) {
              // this.$message.error(res.data.msg)
            } else {
              this.dataForm.id = res.info.id;
              this.dataForm.name = res.info.name;
              this.dataForm.appendixUrl = res.info.appendixUrl;
              this.dataForm.appendixName = res.info.appendixName;
              this.dataForm.orderNum = res.info.orderNum
              //附件
              if (this.dataForm.appendixUrl) {
                let appendixUrl = this.dataForm.appendixUrl.split(',')
                let appendixName = this.dataForm.appendixName.split(',')
                let list = []
                for (let i = 0; i < appendixUrl.length; i++) {
                  list.push({ url: appendixUrl[i], name: appendixName[i] })
                }
                this.fileList = list
              }
            }
          });
        }
      })

    },
    handlePreview(file) {
      console.log(file)
      if(file.response){
        this.videoUrl=this.url+file.response.map.filePath
      }else{
        this.videoUrl=this.url+file.url
      }
      
      this.qrcodeDialogVisible=true
    },
    beforeAvatarUpload(file) {
      console.log(file)
      let hz = file.name.split('.')[(file.name.split('.')).length - 1]
      // const isJPG = file.type === "image/jpg";

      //1MB=1024*1024(1MB=1024KB 1KB=1024MB)
      const is50M = file.size / 1024 / 1024 < 50;
      console.log(hz)
      //限制文件上传类型
      if (hz!='mp4') {
      	this.$message.error("只能上传mp4格式的视频!");
      	return false
      }
      // if (!is50M) {
      // 	this.$message.error("上传视频大小不能超过 50MB!");
      // 	return false
      // }

      return true
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
    },
    handleSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.fileList = fileList
      } else {
        for (let i = 0; i < fileList.length; i++) {
          if (fileList[i].uid == file.uid) {
            fileList.splice(i, 1);
          }
        }
        this.$message({ message: response.msg, type: 'error', duration: 1000 })
      }
    },
    handleChange(file, fileList) {
      this.fileList = fileList;
    },
    // 表单提交
    async dataFormSubmit() {
      this.$refs["dataForm"].validate(async (valid) => {
        if (valid) {
          //附件
          let appendixUrl = []
          let appendixName = []
          console.log(this.fileList)
          if (this.fileList.length) {
            for (let i = 0; i < this.fileList.length; i++) {
              if (this.fileList[i].response) {
                appendixUrl.push(this.fileList[i].response.map.filePath)
              } else {
                appendixUrl.push(this.fileList[i].url)
              }
              appendixName.push(this.fileList[i].name)
            }
          }
          this.dataForm.appendixUrl = appendixUrl.toString()
          this.dataForm.appendixName = appendixName.toString()
          let url = this.dataForm.id
            ? "/syn/service/update"
            : "/syn/service/save";
          this.isDisabled = true;
          await this.Sevice({
            url: url,
            method: "post",
            data: {
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              appendixUrl: this.dataForm.appendixUrl,
              appendixName: this.dataForm.appendixName,
              orderNum: this.dataForm.orderNum,
            },
          }).then((res) => {
            if (res && res.code == 200) {
              this.dialogFormVisible = false;
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.isDisabled = false;
                  this.$emit("getMenuList");
                },
              });
            } else {
              this.isDisabled = false;
            }
          }).catch(error => {
            console.log(error)
            this.isDisabled = false;;
          });
        }
      });
    },
  },
};
</script>

<style scoped>
.mod-menu .menu-list__input>.el-input__inner,
.mod-menu .icon-list__input>.el-input__inner {
  cursor: pointer;
}

.mod-menu__icon-popover {
  width: 458px;
  overflow: hidden;
}

.mod-menu__icon-inner {
  width: 478px;
  max-height: 258px;
  overflow-x: hidden;
  overflow-y: auto;
}

.mod-menu__icon-list {
  width: 458px;
  padding: 0;
  margin: -8px 0 0 -8px;
}

.mod-menu__icon-list>.el-button {
  padding: 8px;
  margin: 8px 0 0 8px;
}

.mod-menu__icon-list>.el-button>span {
  display: inline-block;
  vertical-align: middle;
  width: 18px;
  height: 18px;
  font-size: 18px;
}

.mod-menu .icon-list__tips {
  font-size: 18px;
  text-align: center;
  color: #e6a23c;
  cursor: pointer;
}

.el-tree {
  height: 200px;
  overflow-y: auto;
}
</style>
